package uvt.gui.mainframe.subpanel.addsss;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import uvt.gui.mainframe.object.command.Command;
import uvt.gui.mainframe.object.observer.ConcreteObservable;
import uvt.gui.mainframe.subpanel.PanelAddParent;
import uvt.gui.mainframe.subpanel.addroute.RouteNameTextField;
import uvt.map.PositionsSettings;


public class PanelAddSSS extends PanelAddParent implements ActionListener,Validable,TextFieldGetter {
	
	private JPanel upPanel;
	private JPanel downPanel;
	private JTextField txtLatitude;
	private JTextField txtLongitude;
	private StationNameTextField txtStationName;	
	private RouteNameTextField txtRouteName;
	private StreetNameTextField txtStreetName;
	
	public PanelAddSSS() {
		setBackground(Color.white);
		setLayout(new GridLayout(2,1));
		
		upPanel = new JPanel(new FlowLayout());
		upPanel.setBackground(Color.white);
	    
	    JLabel routeName = new JLabel("Route Name:");
	    upPanel.add(routeName);
	    
	    txtRouteName = new RouteNameTextField();
	    upPanel.add(txtRouteName);
	    
	    JLabel stationName = new JLabel("Station Name:");
	    upPanel.add(stationName);
	    
	    txtStationName = new StationNameTextField();
	    upPanel.add(txtStationName);
	    
	    JLabel streetName = new JLabel("Street Name:");
	    upPanel.add(streetName);
	    
	    txtStreetName = new StreetNameTextField();
	    upPanel.add(txtStreetName);
	    
		add(upPanel);
		
		downPanel = new JPanel(new FlowLayout());
		downPanel.setBackground(Color.white);		
		
		JLabel lblX = new JLabel("Coordinates:");
		downPanel.add(lblX);
		
		txtLatitude = new JTextField();
		txtLatitude.setEditable(false);
		txtLatitude.setColumns(15);
		downPanel.add(txtLatitude);
	    
	    txtLongitude = new JTextField();
	    txtLongitude.setEditable(false);
		txtLongitude.setColumns(15);
		downPanel.add(txtLongitude);
		
		AddFirstStationButton btnAddStation = new AddFirstStationButton(this,this);
		btnAddStation.addActionListener(this);
		downPanel.add(btnAddStation);
		
		AddSegmentButton btnAddSegment = new AddSegmentButton(this,this);
		btnAddSegment.addActionListener(this);
		downPanel.add(btnAddSegment);
		
		AddStreetAndStationButton btnAddSegmentAndStation = new AddStreetAndStationButton(this,this);
		btnAddSegmentAndStation.addActionListener(this);
		downPanel.add(btnAddSegmentAndStation);
		
		CompleteRouteButton btnCompleteRoute = new CompleteRouteButton(this,this);
		btnCompleteRoute.addActionListener(this);
		downPanel.add(btnCompleteRoute);
		
		add(downPanel);		
		
		PositionsSettings posSettings = PositionsSettings.getInstance();
		posSettings.setTxtSegmentLatitude(txtLatitude);
		posSettings.setTxtSegmentLongitude(txtLongitude);
		
		ConcreteObservable.getRoutePanelObservableInstance().attach(txtRouteName);
		ConcreteObservable.getStationNameObservableInstance().attach(txtStationName);
		ConcreteObservable.getStreetNameObservableInstance().attach(txtStreetName);		
	}
	
	public void actionPerformed(ActionEvent ac) {
		((Command)ac.getSource()).execute();
	}
	
	
	public void cleanPanel() {
		map.removeAllMapMarkers();
		map.removeAllMapPolygons();
	}
	
	public void validateCoordinates() throws Exception {
		if(txtLatitude.getText().equals("")) throw new Exception("Latitude field empty!");
		if(txtLongitude.getText().equals("")) throw new Exception("Longitude field empty!");
	}
	
	public void validateForAddFirstStationButton() throws Exception {
		validateCoordinates();
		if(txtStationName.getText().equals("")) throw new Exception("Station Name field empty!");
		
	}
	
	public void validateForAddSegmentButton() throws Exception {
		validateCoordinates();
	}
	
	public void validateForAddStreetAndStationButton() throws Exception {
		validateCoordinates();
		if(txtRouteName.getText().equals("")) throw new Exception("Route Name field empty!");
		if(txtStationName.getText().equals("")) throw new Exception("Station Name field empty!");
		if(txtStreetName.getText().equals("")) throw new Exception("Street Name field empty!");
	}
	
	public void validateForCompleteRouteButton() throws Exception {
		validateCoordinates();
		if(txtRouteName.getText().equals("")) throw new Exception("Route Name field empty!");
		if(txtStreetName.getText().equals("")) throw new Exception("Street Name field empty!");
	}
	
	public String getLatitude() {
		return txtLatitude.getText();
	}
	
	public String getLongitude() {
		return txtLongitude.getText();
	}
	
	public String getRouteName() {
		return txtRouteName.getText();
	}
	
	public String getStationName() {
		return txtStationName.getText();
	}
	
	public String getStreetName() {
		return txtStreetName.getText();
	}

}